﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zsnd.Helpers;
using MaymuseMS.DAL;
using MaymuseMS.Models;

namespace MaymuseMS.Controllers {
    public class SupplierController : GenericController {

        public ActionResult Index() {
            return View();
        }

        [HttpPost]
        public ActionResult Index(JQGridParm parm) {
            string sidx = parm.Sidx;
            string sord = parm.Sord;
            int page = parm.Page;
            int rows = parm.Rows;

            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = unitOfWork.SupplierRepository.dbSet.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            var entities = unitOfWork.SupplierRepository.Get(pageIndex, pageSize, sidx, sord, parm.Filters);

            var enums = entities.Select(e => new {
                id = e.SupplierId,
                cell = new object[] { 
                    e.Number,
                    e.Name,
                    e.WebSiteUrl,
                    e.Contacts.Address, 
                    e.Contacts.Phone, 
                    e.Contacts.Fax, 
                    e.Banks.Name,
                    e.Banks.Account,
                    e.Banks.Number,
                    e.Remark
                }
            });

            var jsonData = new {
                success = true,
                total = totalPages,
                page,
                records = totalRecords,
                rows = enums
            };
            return Json2(jsonData);
        }

        [HttpPost]
        public ActionResult Add(Supplier supplier) {
            Supplier supplierAdd = new Supplier();
            if (TryUpdateModel(supplierAdd)) {
                unitOfWork.SupplierRepository.Add(supplierAdd);
                unitOfWork.Save();
                return Json2(new { success = true });
            }
            return Json2(new { success = false, message = errorMessage });
        }

        [HttpPost]
        public ActionResult Edit(int? id, Supplier supplier) {
            if (id != null) {
                Supplier supplierEdit = unitOfWork.SupplierRepository.Get((int)id);
                if (supplierEdit != null) {
                    if (TryUpdateModel(supplierEdit)) {
                        unitOfWork.Save();
                        return Json2(new { success = true });
                    }
                }
            }
            return Json2(new { success = false, message = errorMessage });
        }

        [HttpPost]
        public ActionResult Delete(string id) {
            if (string.IsNullOrWhiteSpace(id))
                Json2(new { success = false, message = errorMessage });
            try {
                unitOfWork.SupplierRepository.Remove(id);
                unitOfWork.Save();
                return Json2(new { success = true });
            } catch (ArgumentNullException e) {
                return Json2(new { success = false, message = e.Message });
            } catch (Exception e) {
                return Json2(new { success = false, message = errorMessage });
            }
        }

        public ActionResult Suppliers() {
            var suppliers = unitOfWork.SupplierRepository.dbSet.Select(s => new { key = s.SupplierId, value = s.Name }).AsEnumerable();
            var jsonData = new { value = suppliers };
            return Json2(jsonData);
        }

        public ActionResult Get(int id) {
            var p = unitOfWork.SupplierRepository.Get(id);
            if (p == null)
                return Json2(new { success = false, massage = errorMessage });
            return Json2(new { success = true, name = p.Name });
        }
    }
}
